using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using System.Collections.Generic;

using System.Security.Cryptography;
using System.Web.Configuration;

using DnnPerm = DotNetNuke.Security.Permissions;
using DnnRoles = DotNetNuke.Security.Roles;
using DnnFS = DotNetNuke.Services.FileSystem;
using System;
using System.Collections;
using DotNetNuke.Common.Utilities;
using Microsoft.VisualBasic;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Portals;
using System.Text.RegularExpressions;
using DotNetNuke.Entities.Users;

namespace DotNetNuke.Modules.IWebCSharp
{
    [System.Web.Script.Services.ScriptService()]
    [System.Xml.Serialization.XmlInclude(typeof(DotNetNuke.Services.FileSystem.FileInfo))]
    [System.Xml.Serialization.XmlInclude(typeof(DotNetNuke.Services.FileSystem.FolderInfo))]
    public partial class WebService
    {
        //#region '"Info Classes"'

        //[Serializable()]
        //public class IWebFileInfo
        //{
        //    private DnnFS.FileInfo info = new DnnFS.FileInfo();
        //    private DateTime lastModifiedField = DateAndTime.Now;


        //    public IWebFileInfo()
        //    {

        //    }

        //    public IWebFileInfo(int folderId, int fileId, string fileName)
        //    {

        //        this.info.FolderId = folderId;
        //        this.info.FileId = fileId;
        //        this.info.FileName = fileName;


        //    }

        //    public IWebFileInfo(DnnFS.FileInfo info)
        //    {
        //        this.info = info;

        //        this.LastModified = System.IO.File.GetLastWriteTime(this.info.PhysicalPath);


        //    }


        //    /// <remarks/>
        //    public string ContentType
        //    {
        //        get
        //        {
        //            return this.info.ContentType;
        //        }
        //        set
        //        {
        //            this.info.ContentType = value;
        //        }
        //    }


        //    /// <remarks/>
        //    public string Extension
        //    {
        //        get
        //        {
        //            return this.info.Extension;
        //        }
        //        set
        //        {
        //            this.info.Extension = value;
        //        }
        //    }

        //    public DateTime LastModified
        //    {
        //        get
        //        {
        //            return this.lastModifiedField;
        //        }
        //        set
        //        {
        //            this.lastModifiedField = value;
        //        }
        //    }


        //    /// <remarks/>
        //    public int FileId
        //    {
        //        get
        //        {
        //            return this.info.FileId;
        //        }
        //        set
        //        {
        //            this.info.FileId = value;
        //        }
        //    }

        //    /// <remarks/>
        //    public string FileName
        //    {
        //        get
        //        {
        //            return this.info.FileName;
        //        }
        //        set
        //        {
        //            this.info.FileName = value;
        //        }
        //    }

        //    /// <remarks/>
        //    public int FolderId
        //    {
        //        get
        //        {
        //            return this.info.FolderId;
        //        }
        //        set
        //        {
        //            this.info.FolderId = value;
        //        }
        //    }


        //    /// <remarks/>
        //    public int Height
        //    {
        //        get
        //        {
        //            return this.info.Height;
        //        }
        //        set
        //        {
        //            this.info.Height = value;
        //        }
        //    }

        //    /// <remarks/>
        //    public int Size
        //    {
        //        get
        //        {
        //            return this.info.Size;
        //        }
        //        set
        //        {
        //            this.info.Size = value;
        //        }
        //    }

        //    /// <remarks/>
        //    public int Width
        //    {
        //        get
        //        {
        //            return this.info.Width;
        //        }
        //        set
        //        {
        //            this.info.Width = value;
        //        }
        //    }




        //}



        //[Serializable()]
        //public class IWebFolderInfo
        //{
        //    private DnnFS.FolderInfo info = new DnnFS.FolderInfo();

        //    private string folderNameField = string.Empty;


        //    public IWebFolderInfo()
        //    {

        //    }

        //    public IWebFolderInfo(int id, string name)
        //    {
        //        this.info.FolderID = id;
        //        this.folderNameField = name;

        //    }

        //    public IWebFolderInfo(DnnFS.FolderInfo info)
        //    {
        //        this.info = info;

        //    }


        //    public int FolderId
        //    {
        //        get
        //        {
        //            return this.info.FolderID;
        //        }
        //        set
        //        {
        //            this.info.FolderID = value;
        //        }
        //    }

        //    public string FolderName
        //    {
        //        get
        //        {
        //            return folderNameField;
        //        }
        //        set
        //        {
        //            this.folderNameField = value;
        //        }
        //    }



        //}


        //#endregion

        //#region '"Consts"'

        //private const string Pattern_TotalSpaceUsed = "Using: {0}";
        //private const string Pattern_TotalSpaceUsedWithQuota = "Used: {0} of {1}";

        //private const int Setting_MaxRetries = 3000;
        //private const int Setting_MaxSeconds = 10;

        //private const string RegEx_FolderNameWithParents = @"^[a-zA-Z0-9\-\s_\/]{1,250}$";
        //private const string RegEx_FolderNameWithOutParents = @"^[a-zA-Z0-9\-\s_]{1,250}$";
        //private const string RegEx_FileNameWithFolders = @"^[a-zA-Z0-9\-\s_./]{1,250}$";
        //private const string RegEx_FileNameWithOutFolders = @"^[a-zA-Z0-9\-\s_.]{1,250}$";


        //private const int Permission_ReadId = 5;
        //private const int Permission_WriteId = 6;
        //private readonly XmlQualifiedName FileManager_QName = new XmlQualifiedName("FileManager");

        //#endregion

        //#region '"Private Methods"'


        //private static List<IWebFolderInfo> ConvertToGenericListOfFolderInfo(ArrayList folderArray)
        //{

        //    List<IWebFolderInfo> folderList = new List<IWebFolderInfo>();

        //    try
        //    {


        //        foreach (DnnFS.FolderInfo folder in folderArray)
        //        {

        //            folderList.Add(new IWebFolderInfo(folder.FolderID, folder.FolderName));

        //        }

        //    }
        //    catch (InvalidCastException castEx)
        //    {

        //        return null;

        //    }
        //    catch (Exception ex)
        //    {

        //        return null;

        //    }

        //    return folderList;

        //}


        //private static List<IWebFileInfo> ConvertToGenericListOfFileInfo(ArrayList fileArray)
        //{

        //    List<IWebFileInfo> fileList = new List<IWebFileInfo>();

        //    try
        //    {


        //        foreach (DnnFS.FileInfo file in fileArray)
        //        {

        //            fileList.Add(new IWebFileInfo(file));

        //        }

        //    }
        //    catch (InvalidCastException castEx)
        //    {

        //        return null;

        //    }
        //    catch (Exception ex)
        //    {

        //        return null;

        //    }

        //    return fileList;

        //}



        //private static long ConvertToMB(int bytes)
        //{

        //    return System.Convert.ToInt64((bytes / (1024 * 1024)));

        //}


        //private static DotNetNuke.Entities.Portals.PortalSettings GetSettings()
        //{

        //    return PortalController.GetCurrentPortalSettings();

        //}


        //private string CleanErrorMessage(string strErrorMessage)
        //{
        //    string cleanedError;

        //    cleanedError = strErrorMessage.Replace(Server.MapPath(@"~/Portals/" + this.IWebCredentials.PortalID.ToString()), string.Empty);

        //    return cleanedError;

        //}


        //private string CleanPath(string rawPath)
        //{

        //    if (string.IsNullOrEmpty(rawPath))
        //    {
        //        return string.Empty;
        //    }

        //    return Regex.Replace(rawPath, "[.]{2,}", string.Empty);

        //}


        //private bool IsFilePath(string rawPath, bool allowFolders)
        //{

        //    string pattern = string.Empty;

        //    if (allowFolders)
        //    {

        //        pattern = RegEx_FileNameWithFolders;

        //    }
        //    else
        //    {

        //        pattern = RegEx_FileNameWithOutFolders;

        //    }

        //    if (System.Text.RegularExpressions.Regex.IsMatch(rawPath, pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase))
        //    {

        //        return true;

        //    }


        //    return false;



        //}


        //private bool IsFolderPath(string rawPath, bool allowParents)
        //{

        //    if (string.IsNullOrEmpty(rawPath))
        //    {
        //        return true;
        //    }

        //    string pattern;

        //    if (allowParents)
        //    {
        //        pattern = RegEx_FolderNameWithParents;

        //    }
        //    else
        //    {
        //        pattern = RegEx_FolderNameWithOutParents;

        //    }


        //    if (System.Text.RegularExpressions.Regex.IsMatch(rawPath, pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase))
        //    {

        //        return true;

        //    }


        //    return false;

        //}



        //private string GetRelativeFilePath(int fileId)
        //{

        //    DnnFS.FileController fileController = new DnnFS.FileController();

        //    DnnFS.FileInfo file = fileController.GetFileById(fileId, this.IWebCredentials.PortalID);

        //    return System.IO.Path.Combine(file.Folder, file.FileName);

        //}


        //private string GetRelativeFilePath(int folderId, string fileName)
        //{

        //    string cleanFileName = System.IO.Path.GetFileName(this.CleanPath(fileName));

        //    return System.IO.Path.Combine(this.GetRelativeFolderPath(folderId), cleanFileName);

        //}


        //private string GetFileMapPath(int fileId)
        //{

        //    IWebFileInfo info = this.GetFileById(fileId);

        //    return this.GetFileMapPath(info.FolderId, info.FileName);

        //}


        //private string GetFileMapPath(int folderId, string fileName)
        //{

        //    return Server.MapPath(GetSettings().HomeDirectory + this.GetRelativeFilePath(folderId, fileName));

        //}


        //private int GetHomeDirectoryFolderId()
        //{


        //    DnnFS.FolderController folderController = new DnnFS.FolderController();

        //    DnnFS.FolderInfo folder = FileSystemUtils.GetFolder(this.IWebCredentials.PortalID, string.Empty);

        //    if (folder != null)
        //    {

        //        return folder.FolderID;

        //    }
        //    else
        //    {

        //        return -1;

        //    }


        //}


        //private string GetRelativeFolderPath(int folderId)
        //{

        //    DnnFS.FolderController folderControl = new DnnFS.FolderController();

        //    if (folderId < 1)
        //    {

        //        return string.Empty;

        //    }

        //    DotNetNuke.Services.FileSystem.FolderInfo folder;

        //    folder = folderControl.GetFolderInfo(this.IWebCredentials.PortalID, folderId);

        //    if (folder != null)
        //    {

        //        return folder.FolderPath;

        //    }
        //    else
        //    {

        //        return string.Empty;

        //    }


        //}


        //private string GetFolderMapPath(int folderId)
        //{

        //    string relativePath = this.GetRelativeFolderPath(folderId);

        //    if (!(string.IsNullOrEmpty(relativePath)))
        //    {

        //        return Server.MapPath(GetSettings().HomeDirectory + relativePath);

        //    }
        //    else
        //    {

        //        return GetSettings().HomeDirectoryMapPath;

        //    }

        //}



        //private SoapException GetLoggedNotAuthorizedEx()
        //{

        //    System.Security.SecurityException dnnEx = new System.Security.SecurityException("Not Authorized to preform this function: " + " Username: " + this.IWebCredentials.Username);


        //    Exceptions.LogException(dnnEx);


        //    SoapException soapEx = new SoapException("Not Authorized to preform this function. Username: " + this.IWebCredentials.Username, FileManager_QName);

        //    return soapEx;

        //}



        //private bool HasFileAccess(int fileId, int permissionType)
        //{


        //    DnnFS.FileController fileController = new DnnFS.FileController();

        //    DnnFS.FileInfo file = fileController.GetFileById(fileId, this.IWebCredentials.PortalID);

        //    return this.HasFolderAccess(file.FolderId, permissionType);

        //}


        //private bool HasFolderAccess(int folderId, int permissionType)
        //{


        //    UserInfo user = UserController.GetUserByName(this.IWebCredentials.PortalID, this.IWebCredentials.Username, false);

        //    this.IWebCredentials.UserID = user.UserID;

        //    if (user.IsSuperUser)
        //    {
        //        return true;
        //    }

        //    DnnRoles.RoleController roleCtrl = new DnnRoles.RoleController();

        //    user.Roles = roleCtrl.GetRolesByUser(user.UserID, this.IWebCredentials.PortalID);

        //    DnnPerm.FolderPermissionController ctrl = new DnnPerm.FolderPermissionController();

        //    DnnPerm.FolderPermissionCollection folderPermsList;

        //    folderPermsList = ctrl.GetFolderPermissionsCollectionByFolderPath(this.IWebCredentials.PortalID, this.GetRelativeFolderPath(folderId));

        //    bool allowed = false;

        //    foreach (DnnPerm.FolderPermissionInfo perm in folderPermsList)
        //    {


        //        if (perm.AllowAccess && perm.PermissionID == permissionType && (perm.UserID == this.IWebCredentials.UserID || user.IsInRole(perm.RoleName)))
        //        {

        //            allowed = true;

        //        }
        //        else if (!(perm.AllowAccess) && perm.PermissionID == permissionType && (perm.UserID == this.IWebCredentials.UserID || user.IsInRole(perm.RoleName)))
        //        {

        //            allowed = false;
        //            break; /* TRANSWARNING: check that break is in correct scope */

        //        }

        //    }

        //    return allowed;

        //}



        //private void SetHeader(int PortalID, int UserID, string Username, string Password, string ModuleId, bool WebPageCall, bool Encrypted)
        //{

        //    this.IWebCredentials = new IWebAuthendicationHeader();

        //    this.IWebCredentials.PortalID = PortalID;
        //    this.IWebCredentials.UserID = UserID;
        //    this.IWebCredentials.Username = Username;
        //    this.IWebCredentials.Password = Password;
        //    this.IWebCredentials.ModuleId = Convert.ToInt32(ModuleId);
        //    this.IWebCredentials.WebPageCall = Convert.ToString(WebPageCall);
        //    this.IWebCredentials.Encrypted = Convert.ToString(Encrypted);

        //}


        //private void SyncFilesSystem()
        //{

        //    FileSystemUtils.SynchronizeFolder(this.IWebCredentials.PortalID, GetSettings().HomeDirectoryMapPath, string.Empty, true);
        //}


        //#endregion

        //#region '"Web Methods"'

        //[System.Web.Script.Services.ScriptMethod(), WebMethod(MessageName = "AddFolderAjax", Description = "Add a new folder to specified parent or portal root. Ajax Overload. *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! ")]
        //public void AddFolderAjax(int portalid, int userId, string username, string password, int moduleId, bool webPageCall, bool encrypted, int parentFolderId, string newFolderName)
        //{

        //    this.SetHeader(portalid, userId, username, password, Convert.ToString(moduleId), webPageCall, encrypted);

        //    this.AddFolder(parentFolderId, newFolderName);

        //}

        //[WebMethod(Description = "Add a new folder to specified parent or portal root. *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public void AddFolder(int folderId, string newFolderName)
        //{


        //    IWebAuthendication authendication = new IWebAuthendication(this.IWebCredentials);

        //    if (authendication.ValidAndAuthorized() && this.HasFolderAccess(folderId, Permission_WriteId))
        //    {


        //        string parentFolder = this.GetFolderMapPath(folderId);


        //        try
        //        {

        //            FileSystemUtils.AddFolder(GetSettings(), parentFolder, newFolderName);

        //        }
        //        catch (Exception ex)
        //        {

        //            throw new SoapException(CleanErrorMessage(ex.Message), FileManager_QName, ex);

        //        }

        //    }
        //    else
        //    {


        //        throw this.GetLoggedNotAuthorizedEx();

        //    }

        //}


        //[System.Web.Script.Services.ScriptMethod(), WebMethod(MessageName = "CopyFileAjax", Description = "Copies a file to a new file with the specified name. Ajax Overload. *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public string CopyFileAjax(int portalid, int userId, string username, string password, int moduleId, bool webPageCall, bool encrypted, int sourceFileID, int destFolderId, string destFileName)
        //{
        //    this.SetHeader(portalid, userId, username, password, Convert.ToString(moduleId), webPageCall, encrypted);

        //    return CopyFile(sourceFileID, destFolderId, destFileName);
        //}

        //[WebMethod(Description = "Copies a file to a new file with the specified name. *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public string CopyFile(int sourceFileID, int destFolderId, string destFileName)
        //{

        //    IWebAuthendication authendication = new IWebAuthendication(this.IWebCredentials);

        //    if (authendication.ValidAndAuthorized() && this.HasFileAccess(sourceFileID, Permission_ReadId) && this.HasFolderAccess(destFolderId, Permission_WriteId))
        //    {

        //        string sourceFile = this.GetRelativeFilePath(sourceFileID);

        //        destFileName = this.GetRelativeFilePath(destFolderId, destFileName);

        //        try
        //        {

        //            return FileSystemUtils.CopyFile(sourceFile, destFileName, PortalController.GetCurrentPortalSettings());

        //        }
        //        catch (Exception ex)
        //        {

        //            throw new SoapException(CleanErrorMessage(ex.Message), FileManager_QName, ex);

        //        }


        //    }
        //    else
        //    {
        //        throw this.GetLoggedNotAuthorizedEx();


        //    }

        //}


        //[System.Web.Script.Services.ScriptMethod(), WebMethod(MessageName = "DefaultProtectedFoldersAjax", Description = "Determines if specified path is the default protected folder. Ajax Overload. *DotNetNuke* |IWeb Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public bool DefaultProtectedFoldersAjax(int portalid, int userId, string username, string password, int moduleId, bool webPageCall, bool encrypted, int folderId)
        //{

        //    this.SetHeader(portalid, userId, username, password, Convert.ToString(moduleId), webPageCall, encrypted);


        //    return DefaultProtectedFolders(folderId);
        //}

        //[WebMethod(Description = "Determines if specified path is the default protected folder. *DotNetNuke* |IWeb Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public bool DefaultProtectedFolders(int folderId)
        //{

        //    IWebAuthendication authendication = new IWebAuthendication(this.IWebCredentials);

        //    if (authendication.ValidAndAuthorized() && this.HasFolderAccess(folderId, Permission_ReadId))
        //    {


        //        try
        //        {

        //            string folderPath = this.GetRelativeFolderPath(folderId);


        //            return FileSystemUtils.DefaultProtectedFolders(folderPath);

        //        }
        //        catch (Exception ex)
        //        {

        //            throw new SoapException(CleanErrorMessage(ex.Message), FileManager_QName, ex);

        //        }

        //    }
        //    else
        //    {


        //        throw this.GetLoggedNotAuthorizedEx();

        //    }


        //}


        //[System.Web.Script.Services.ScriptMethod(), WebMethod(MessageName = "DeleteFilesAjax", Description = " Delete the list of specified files. Ajax Overload.  *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public string DeleteFilesAjax(int portalid, int userId, string username, string password, int moduleId, bool webPageCall, bool encrypted, int[] files)
        //{
        //    this.SetHeader(portalid, userId, username, password, Convert.ToString(moduleId), webPageCall, encrypted);

        //    return DeleteFiles(files);
        //}

        //[WebMethod(Description = " Delete the list of specified files *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public string DeleteFiles(int[] files)
        //{

        //    IWebAuthendication authendication = new IWebAuthendication(this.IWebCredentials);

        //    if (!(authendication.ValidAndAuthorized()))
        //    {

        //        throw this.GetLoggedNotAuthorizedEx();

        //    }


        //    string strErrorMessage = string.Empty;

        //    try
        //    {

        //        int i = 0;
        //        if (files.Length == 0)
        //        {

        //            return "0";

        //            // TRANSWARNING: Unreachable code detected and removed 
        //        }

        //        string strCurError = string.Empty;

        //        for (i = 0; i <= files.Length - 1; i++)
        //        {

        //            bool timedOut = false;

        //            if (this.HasFileAccess(files[i], Permission_WriteId))
        //            {

        //                bool deleted = false;

        //                DateTime startTime = DateAndTime.Now;


        //                while (!(deleted || timedOut))
        //                {


        //                    try
        //                    {

        //                        FileSystemUtils.DeleteFile(this.GetRelativeFilePath(files[i]), GetSettings());

        //                        System.IO.File.Delete(this.GetFileMapPath(files[i]));

        //                        deleted = true;


        //                    }
        //                    catch (Exception ex)
        //                    {

        //                        deleted = false;

        //                        timedOut = DateAndTime.DateAdd(DateInterval.Second, Setting_MaxSeconds, startTime) < DateAndTime.Now;

        //                        if (timedOut)
        //                        {

        //                            strCurError += ex.Message + Constants.vbCrLf;

        //                            break; /* TRANSWARNING: check that break is in correct scope */

        //                        }

        //                    }

        //                }

        //            }
        //            else
        //            {

        //                strCurError = "Access denied. Please check file permissions and try again.";

        //            }

        //            if (!(string.IsNullOrEmpty(strCurError)) || timedOut)
        //            {

        //                strErrorMessage += " Error Deleting File: " + strCurError;

        //            }

        //        }


        //        this.SyncFilesSystem();


        //    }
        //    catch (Exception ex)
        //    {

        //        throw new SoapException(CleanErrorMessage(ex.Message), FileManager_QName, ex);

        //    }

        //    if (!(string.IsNullOrEmpty(strErrorMessage)))
        //    {


        //        throw new SoapException(CleanErrorMessage(strErrorMessage), FileManager_QName);

        //    }

        //    return files.Length.ToString();

        //}


        //[System.Web.Script.Services.ScriptMethod(), WebMethod(MessageName = "DownloadFileAjax", Description = "Download the specified file.  Ajax Overload. *DotNetNuke* |IWeb Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public byte[] DownloadFileAjax(int portalid, int userId, string username, string password, int moduleId, bool webPageCall, bool encrypted, int fileId)
        //{


        //    this.SetHeader(portalid, userId, username, password, Convert.ToString(moduleId), webPageCall, encrypted);

        //    return DownloadFile(fileId);
        //}

        //[WebMethod(Description = "Download the specified file. *DotNetNuke* |IWeb Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public byte[] DownloadFile(int fileId)
        //{

        //    IWebAuthendication authendication = new IWebAuthendication(this.IWebCredentials);

        //    if (authendication.ValidAndAuthorized() && this.HasFileAccess(fileId, Permission_ReadId))
        //    {

        //        DotNetNuke.Services.FileSystem.FileInfo fileLocInfo;

        //        DotNetNuke.Services.FileSystem.FileController fileCtrl = new DotNetNuke.Services.FileSystem.FileController();

        //        IWebFileInfo info = this.GetFileById(fileId);

        //        fileLocInfo = fileCtrl.GetFile(System.IO.Path.GetFileName(info.FileName), this.IWebCredentials.PortalID, info.FolderId);

        //        return FileSystemUtils.GetFileContent(fileLocInfo);

        //    }
        //    else
        //    {

        //        throw new SoapException("Not Authorized to preform this function", FileManager_QName);

        //    }

        //}


        //[System.Web.Script.Services.ScriptMethod(), WebMethod(MessageName = "GetFilesByFolderAjax", Description = " Get a list of files in the folder supplied.  Ajax Overload. *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public List<IWebFileInfo> GetFilesByFolderAjax(int portalid, int userId, string username, string password, int moduleId, bool webPageCall, bool encrypted, int folderId)
        //{

        //    this.SetHeader(portalid, userId, username, password, Convert.ToString(moduleId), webPageCall, encrypted);

        //    return GetFilesByFolder(folderId);
        //}

        //[WebMethod(Description = " Get a list of files in the folder supplied. *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public List<IWebFileInfo> GetFilesByFolder(int folderId)
        //{

        //    IWebAuthendication authendication = new IWebAuthendication(this.IWebCredentials);

        //    if (authendication.ValidAndAuthorized() && this.HasFolderAccess(folderId, Permission_ReadId))
        //    {


        //        try
        //        {

        //            List<IWebFileInfo> files;


        //            if (folderId < 1)
        //            {
        //                folderId = this.GetHomeDirectoryFolderId();
        //            }

        //            files = ConvertToGenericListOfFileInfo(FileSystemUtils.GetFilesByFolder(this.IWebCredentials.PortalID, folderId));

        //            return files;

        //        }
        //        catch (Exception ex)
        //        {

        //            throw new SoapException(CleanErrorMessage(ex.Message), FileManager_QName, ex);

        //        }

        //    }
        //    else
        //    {

        //        throw this.GetLoggedNotAuthorizedEx();


        //    }


        //}


        //[System.Web.Script.Services.ScriptMethod(), WebMethod(MessageName = "GetFileByIdAjax", Description = " Get a FileInfo object for the specified FileId.  Ajax Overload. *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public IWebFileInfo GetFileByIdAjax(int portalid, int userId, string username, string password, int moduleId, bool webPageCall, bool encrypted, int fileId)
        //{

        //    this.SetHeader(portalid, userId, username, password, Convert.ToString(moduleId), webPageCall, encrypted);

        //    return GetFileById(fileId);
        //}

        //[WebMethod(Description = " Get a FileInfo object for the specified FileId. *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public IWebFileInfo GetFileById(int fileId)
        //{


        //    IWebAuthendication authendication = new IWebAuthendication(this.IWebCredentials);

        //    if (authendication.ValidAndAuthorized() && this.HasFileAccess(fileId, Permission_ReadId))
        //    {


        //        DnnFS.FileController fileController = new DnnFS.FileController();

        //        DnnFS.FileInfo file = fileController.GetFileById(fileId, this.IWebCredentials.PortalID);

        //        return new IWebFileInfo(file);

        //    }
        //    else
        //    {

        //        throw this.GetLoggedNotAuthorizedEx();

        //    }


        //}


        //[System.Web.Script.Services.ScriptMethod()]
        //[WebMethod(Description = " Get sub folders by parent FolderId.  Ajax Overload. *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public List<IWebFolderInfo> GetFoldersByParentAjax(int portalid, int userId, string username, string password, int moduleId, bool webPageCall, bool encrypted, int parentFolderId)
        //{

        //    this.SetHeader(portalid, userId, username, password, Convert.ToString(moduleId), webPageCall, encrypted);

        //    return GetFoldersByParent(parentFolderId);

        //}

        //[WebMethod(Description = " Get sub folders by parent FolderId. *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public List<IWebFolderInfo> GetFoldersByParent(int parentFolderId)
        //{

        //    IWebAuthendication authendication = new IWebAuthendication(this.IWebCredentials);

        //    if (authendication.ValidAndAuthorized() && this.HasFolderAccess(parentFolderId, Permission_ReadId))
        //    {



        //        try
        //        {

        //            string parentFolder = this.GetRelativeFolderPath(parentFolderId);

        //            return ConvertToGenericListOfFolderInfo(FileSystemUtils.GetFoldersByParentFolder(this.IWebCredentials.PortalID, parentFolder));

        //        }
        //        catch (Exception ex)
        //        {

        //            throw new SoapException(CleanErrorMessage(ex.Message), FileManager_QName, ex);

        //        }

        //    }
        //    else
        //    {

        //        throw this.GetLoggedNotAuthorizedEx();


        //    }


        //}


        //[System.Web.Script.Services.ScriptMethod()]
        //[WebMethod(Description = " Get a FolderInfo object for the specified FolderId. Ajax Overload. *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public IWebFolderInfo GetFolderByIdAjax(int portalid, int userId, string username, string password, int moduleId, bool webPageCall, bool encrypted, int folderId)
        //{
        //    this.SetHeader(portalid, userId, username, password, Convert.ToString(moduleId), webPageCall, encrypted);

        //    return GetFolderById(folderId);
        //}

        //[WebMethod(Description = " Get a FolderInfo object for the specified FolderId. *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public IWebFolderInfo GetFolderById(int folderId)
        //{

        //    IWebAuthendication authendication = new IWebAuthendication(this.IWebCredentials);

        //    if (authendication.ValidAndAuthorized() && this.HasFolderAccess(folderId, Permission_ReadId))
        //    {


        //        DnnFS.FolderController folderController = new DnnFS.FolderController();

        //        DnnFS.FolderInfo folder = folderController.GetFolderInfo(folderId, this.IWebCredentials.PortalID);

        //        return new IWebFolderInfo(folder);

        //    }
        //    else
        //    {

        //        throw new SoapException("Not Authorized to preform this function", FileManager_QName);

        //    }

        //}


        //[System.Web.Script.Services.ScriptMethod()]
        //[WebMethod(Description = " Gets the maximum request length in KBs. Return -1 if configuration could not be read. Ajax Overload. *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public long GetMaxRequestLengthAjax(int portalid, int userId, string username, string password, int moduleId, bool webPageCall, bool encrypted)
        //{

        //    this.SetHeader(portalid, userId, username, password, Convert.ToString(moduleId), webPageCall, encrypted);

        //    return this.GetMaxRequestLength();

        //}

        //[WebMethod(Description = " Gets the maximum request length in KBs. Return -1 if configuration could not be read. *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public long GetMaxRequestLength()
        //{


        //    IWebAuthendication authendication = new IWebAuthendication(this.IWebCredentials);

        //    if (authendication.ValidAndAuthorized() && this.HasFolderAccess(this.GetHomeDirectoryFolderId(), Permission_ReadId))
        //    {

        //        try
        //        {

        //            System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~/DesktopModules/IWeb/Web.config");

        //            if (config == null)
        //            {
        //                return -1;
        //            }

        //            HttpRuntimeSection reuntimeSection = null;

        //            reuntimeSection = config.GetSection("system.web/httpRuntime") as HttpRuntimeSection;

        //            if (reuntimeSection == null)
        //            {
        //                return -1;
        //            }


        //            return reuntimeSection.MaxRequestLength;


        //        }
        //        catch (Exception ex)
        //        {

        //            return -1;

        //        }

        //    }
        //    else
        //    {

        //        throw this.GetLoggedNotAuthorizedEx();

        //    }

        //}


        //[System.Web.Script.Services.ScriptMethod()]
        //[WebMethod(Description = " Get portal root folders.  Ajax Overload. *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public List<IWebFolderInfo> GetPortalRootFoldersAjax(int portalid, int userId, string username, string password, int moduleId, bool webPageCall, bool encrypted)
        //{

        //    this.SetHeader(portalid, userId, username, password, Convert.ToString(moduleId), webPageCall, encrypted);

        //    return GetPortalRootFolders();

        //}

        //[WebMethod(Description = " Get portal root folders. *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public List<IWebFolderInfo> GetPortalRootFolders()
        //{


        //    IWebAuthendication authendication = new IWebAuthendication(this.IWebCredentials);

        //    if (authendication.ValidAndAuthorized() && this.HasFolderAccess(this.GetHomeDirectoryFolderId(), Permission_ReadId))
        //    {

        //        try
        //        {

        //            return ConvertToGenericListOfFolderInfo(FileSystemUtils.GetFolders(this.IWebCredentials.PortalID));

        //        }
        //        catch (Exception ex)
        //        {

        //            throw new SoapException(CleanErrorMessage(ex.Message), FileManager_QName, ex);

        //        }

        //    }
        //    else
        //    {

        //        throw this.GetLoggedNotAuthorizedEx();

        //    }

        //}


        //[System.Web.Script.Services.ScriptMethod(), WebMethod(Description = " Get the total space used by the portal folder.  Ajax Overload. *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public string GetPortalSpaceUsedAjax(int portalid, int userId, string username, string password, int moduleId, bool webPageCall, bool encrypted, bool showQuota)
        //{

        //    this.SetHeader(portalid, userId, username, password, Convert.ToString(moduleId), webPageCall, encrypted);

        //    return GetPortalSpaceUsed(showQuota);
        //}

        //[WebMethod(Description = " Get the total space used by the portal folder. *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public string GetPortalSpaceUsed(bool showQuota)
        //{


        //    IWebAuthendication authendication = new IWebAuthendication(this.IWebCredentials);

        //    if (authendication.ValidAndAuthorized())
        //    {

        //        string strUsed = null;

        //        PortalController portalCtrl = new PortalController();


        //        long spaceUsed = portalCtrl.GetPortalSpaceUsedBytes(this.IWebCredentials.PortalID);

        //        if (spaceUsed < 1024)
        //        {
        //            strUsed = spaceUsed.ToString("0.00") + "B";
        //        }
        //        else if (spaceUsed < (1024 * 1024))
        //        {
        //            strUsed = (spaceUsed / 1024).ToString("0.00") + "KB";
        //        }
        //        else
        //        {
        //            strUsed = (ConvertToMB(Convert.ToInt32(spaceUsed))).ToString("0.00") + "MB";
        //        }

        //        if (showQuota)
        //        {


        //            string hostingSpace = null;

        //            int hostingSpaceMB = GetSettings().HostSpace;

        //            if (hostingSpaceMB > 0)
        //            {

        //                hostingSpace = string.Format("{0}MB", hostingSpaceMB);

        //            }
        //            else
        //            {

        //                hostingSpace = "[unlimited]";

        //            }


        //            return string.Format(Pattern_TotalSpaceUsedWithQuota, strUsed, hostingSpace);


        //        }
        //        else
        //        {

        //            return string.Format(Pattern_TotalSpaceUsed, strUsed);

        //        }


        //    }
        //    else
        //    {

        //        throw this.GetLoggedNotAuthorizedEx();
        //    }

        //}


        //[System.Web.Script.Services.ScriptMethod()]
        //[WebMethod(Description = " Upload as file. Ajax Overload.  *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public string UploadFileAjax(int portalid, int userId, string username, string password, int moduleId, bool webPageCall, bool encrypted, int folderId, string fileName, byte[] bytes)
        //{
        //    this.SetHeader(portalid, userId, username, password, Convert.ToString(moduleId), webPageCall, encrypted);

        //    return UploadFile(folderId, fileName, bytes);
        //}


        //[WebMethod(Description = " Upload as file *DotNetNuke* |IWEB Core| #IWEB FileManager# !Portal! "), SoapHeader("IWebCredentials")]
        //public string UploadFile(int folderId, string fileName, byte[] bytes)
        //{


        //    PortalController portalCtrl = new PortalController();

        //    long spaceUsed = portalCtrl.GetPortalSpaceUsedBytes(this.IWebCredentials.PortalID);

        //    long spaceUsedWithFile = System.Convert.ToInt64((ConvertToMB(Convert.ToInt32(spaceUsed)) + ConvertToMB(Convert.ToInt32((bytes.Length).ToString("0.00")))));


        //    long hostingSpace = ((long)(GetSettings().HostSpace));

        //    if (hostingSpace == 0 || spaceUsedWithFile < hostingSpace)
        //    {

        //        IWebAuthendication authendication = new IWebAuthendication(this.IWebCredentials);

        //        if (authendication.ValidAndAuthorized() && this.HasFolderAccess(folderId, Permission_WriteId))
        //        {

        //            try
        //            {

        //                string destFilePath = null;

        //                destFilePath = this.GetFileMapPath(folderId, fileName);


        //                System.IO.FileStream newFileWriter = null;

        //                try
        //                {

        //                    newFileWriter = new System.IO.FileStream(destFilePath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);

        //                    newFileWriter.Write(bytes, 0, bytes.Length);


        //                }
        //                catch (Exception ex)
        //                {

        //                    Exceptions.LogException(ex);

        //                    throw new SoapException("Error uploading " + fileName + ". " + "Please check your site logs for details.", FileManager_QName);

        //                }
        //                finally
        //                {

        //                    if (newFileWriter != null)
        //                    {


        //                        newFileWriter.Flush();

        //                        newFileWriter.Close();

        //                        newFileWriter.Dispose();

        //                        newFileWriter = null;

        //                        bytes = null;

        //                    }
        //                }

        //                this.SyncFilesSystem();

        //                return this.GetPortalSpaceUsed(true);

        //            }
        //            catch (SoapException soapEx)
        //            {

        //                throw;

        //            }
        //            catch (Exception ex)
        //            {

        //                Exceptions.LogException(ex);

        //                throw new SoapException(CleanErrorMessage(ex.Message), FileManager_QName);

        //            }


        //        }
        //        else
        //        {

        //            throw this.GetLoggedNotAuthorizedEx();

        //        }



        //    }
        //    else
        //    {

        //        SoapException soapEx = new SoapException("Portal disk space quota has been reached, upload has been cancelled. " + this.GetPortalSpaceUsed(true), FileManager_QName);

        //        Exceptions.LogException(soapEx);

        //        throw soapEx;

        //    }

        //}


        //#endregion
    }
}
